package com.oocl.im.gui;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.omg.CORBA.FREE_MEM;

import com.oocl.im.clinet.Client;

public class ChatFrame extends JFrame implements ActionListener {
	private Client client;
	private JTextArea chatTa;
	private DefaultListModel<String> friendsDlm;
	private DefaultListModel<String> friendsDlmE;
	private JList<String> friendsJl;
	private JList<String> friendsJlE;
	private JScrollPane friendsJsp;
	private JScrollPane friendsJspE;
	private JTextField sendTf;
	private JButton sendBtn;
	private JButton shakeBtn;
	private long lastTime;

	public JTextArea getChatTa() {
		return chatTa;
	}

	public ChatFrame(String name) {
		this.setSize(400, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		init();
		addEvent();
		this.setVisible(true);
		try {
			client = new Client(name, this);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void init() {
		this.setLayout(new BorderLayout());
		JPanel panLeft = new JPanel(new BorderLayout());
		JPanel panRight = new JPanel(new BorderLayout());
		panRight.setPreferredSize((new Dimension(100, 150)));
		JPanel panLeftBottom = new JPanel(new BorderLayout());
		JPanel panBtnGroup = new JPanel(new BorderLayout());
		chatTa = new JTextArea(10, 20);
		lastTime = 0;

		friendsDlm = new DefaultListModel<>();
		friendsDlmE = new DefaultListModel<>();
		friendsJl = new JList<>();
		friendsJlE = new JList<>();
		friendsJl.setForeground(Color.GREEN);
		friendsJlE.setForeground(Color.GRAY);
		friendsJl.setModel(friendsDlm);
		friendsJlE.setModel(friendsDlmE);
		friendsJsp = new JScrollPane(friendsJl);
		friendsJsp.setBorder(new TitledBorder("On line"));
		friendsJspE = new JScrollPane(friendsJlE);
		friendsJspE.setBorder(new TitledBorder("Off line"));

		sendTf = new JTextField();
		sendBtn = new JButton("send");
		shakeBtn = new JButton("shake");

		sendTf.setFont(new Font("宋体", Font.BOLD, 18));
		sendTf.setForeground(Color.red);

		chatTa.setFont(new Font("宋体", Font.BOLD, 18));
		chatTa.setForeground(Color.blue);
		panRight.add(friendsJsp, BorderLayout.CENTER);
		panRight.add(friendsJspE, BorderLayout.SOUTH);
		panLeft.add(new JScrollPane(chatTa), BorderLayout.CENTER);
		panLeft.add(panLeftBottom, BorderLayout.SOUTH);
		panRight.setBorder(new LineBorder(Color.red));

		this.add(panLeft, BorderLayout.CENTER);
		this.add(panRight, BorderLayout.EAST);
		panBtnGroup.add(shakeBtn, BorderLayout.NORTH);
		panBtnGroup.add(sendBtn, BorderLayout.SOUTH);
		panLeftBottom.add(sendTf, BorderLayout.CENTER);
		panLeftBottom.add(panBtnGroup, BorderLayout.EAST);
	}

	private void addEvent() {
		addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowClosed(WindowEvent e) {
				client.exit();
			}

			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}
		});
		sendBtn.addActionListener(this);
		friendsJl.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				String receiver = friendsJl.getSelectedValue();
				client.setReceiver(receiver);

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub

			}
		});

		shakeBtn.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				long curTime = System.currentTimeMillis();
				if(curTime-lastTime>3000){
					lastTime = curTime;
					client.shake();
				}else{
					chatTa.append("System hint: Can only shake once in three seconds !!\n");
					return;
				}
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}

	// send message to other
	@Override
	public void actionPerformed(ActionEvent e) {
		String message = sendTf.getText();
		client.chat(message);
		sendTf.setText("");
	}

	public void setFriendList(final Map<String, Boolean> onlineInfo) {
		SwingUtilities.invokeLater(new Runnable() {   

			@Override
			public void run() {
				// TODO Auto-generated method stub
				friendsDlm.clear();
				friendsDlmE.clear();
				for (Entry<String, Boolean> entry : onlineInfo.entrySet()) {
					if(entry.getKey().equals(client.getSender())){
					}else if (entry.getValue()) {
						friendsDlm.addElement(entry.getKey());
					} else {
						friendsDlmE.addElement(entry.getKey());
					}
				}	
			}  
        });
	}

	public void setChatTa(String text) {
		chatTa.setText(text);
	}

	public void exit(String message) {
		dispose();
		new ErrorFrame(message);
	}

	public void setTitle(String sender, String receiver) {
		this.setTitle("you(" + sender + ") chat with " + receiver);
	}

	public void shakeFrame1() throws AWTException {
		final long start = System.currentTimeMillis();
		final long stop = start + 1500;
		Robot r = new Robot();
		long cur;
		Point point = getLocation();
		int x = point.x;
		int y = point.y;
		while ((cur = System.currentTimeMillis()) < stop) {
			r.delay(30);
			setLocation(x + 13, y - 13);
			r.delay(30);
			setLocation(x - 13, y + 13);
		}
		setLocation(x, y);
	}
}
